﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BonSaiTableAdapters;
using Telerik.Web.UI;

public partial class admin_ServiceRepresentImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            RadUpload1.AllowedFileExtensions = new string[] { ".JPG", ".PNG" };
            if (Request.QueryString["serviceID"] != null)
            {
                try
                {
                    int serviceID = Convert.ToInt32(Request.QueryString["serviceID"].ToString());
                    ViewState["serviceID"] = serviceID;
                    FillImage();
                }
                catch (Exception ex)
                {
                    Response.Redirect("ServiceManage.aspx");
                }
            }
            else
            {
                Response.Redirect("ServiceManage.aspx");
            }
        }
    }

    private void FillImage()
    {
        if (ViewState["serviceID"] != null)
        {
            int serviceID = Int32.Parse(ViewState["serviceID"].ToString());
            BonSai.ServiceDataTable serviceDT = new ServiceTableAdapter().GetServiceByID(serviceID);
            if (serviceDT.Rows.Count > 0)
            {
                BonSai.ServiceRow serviceRow = serviceDT.Rows[0] as BonSai.ServiceRow;
                if (!serviceRow.IsRepresentImageNull())
                {
                    imgServiceRepresent.ImageUrl = "~/img_service/" + serviceRow.RepresentImage;
                }
                lblServiceID.Text = serviceID.ToString();
                if (!serviceRow.IsServiceNameNull())
                {
                    lblServiceName.Text = serviceRow.ServiceName;
                }
                else
                {
                    Response.Redirect("ServiceManage.aspx");
                }
            }

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (RadUpload1.InvalidFiles.Count > 0)
        {
            lblImageType.Text = "Bạn chỉ được nhập loại  file .JPG & .PNG !";
            return;
        }

        if (ViewState["serviceID"] != null)
        {
            int serviceID = Convert.ToInt32(ViewState["serviceID"].ToString());


            ///THEM FILE MOI
            ServiceTableAdapter serviceDA = new ServiceTableAdapter();
            string representImage = null;

            if (RadUpload1.UploadedFiles.Count > 0)
            {
                foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(RadUpload1.UploadedFiles[0].FileName);
                    String oldFileName = f.Name;
                    String newFileName = Server.MapPath(".") + "\\..\\img_service\\" + serviceID + f.Extension;
                    if (System.IO.File.Exists(newFileName))
                        System.IO.File.Delete(newFileName);

                    System.IO.File.Move(Server.MapPath(".") + "\\..\\img_service\\" + f.Name, newFileName);
                    representImage = serviceID + f.Extension;
                }
            }
            serviceDA.UpdateRepresentImageByServiceID(representImage, serviceID);
            Response.Redirect("ServiceDetails.aspx?serviceID=" + serviceID + "&code=image");
        }
        else
            return;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (ViewState["serviceID"] != null)
        {
            int productID = Convert.ToInt32(ViewState["serviceID"].ToString());
            Response.Redirect("ServiceDetails.aspx?serviceID=" + productID);
        }
    }
}
